package semantic;

import syntaxtree.*;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;


public class Method {
	public ArrayList<Type> paramsTypes;
	public HashMap<Identifier,Type> params;
	public HashMap<Identifier,Type> locals;
	public Identifier name;
	public Type returnType;
	public Class father;
	
	public Method(Identifier name, Type type, Class father) {
		this.father = father;
		this.name = name;
		this.returnType = type;
		paramsTypes = new ArrayList<Type>();
		params = new HashMap<Identifier,Type>();
		locals = new HashMap<Identifier,Type>();
	}

	public String getName() {
		return name.toString();
	}
	
	public String getFatherName() {
		return father.getName();
	}
	
	public ArrayList<Type> paramsTypes() {
		return paramsTypes;
	}
	
	public Type getType() {
		return returnType;
	}
	
	public Type getLocal(Identifier i) {
		Iterator<Identifier> it = locals.keySet().iterator();
		while(it.hasNext()) {
			Identifier id = (Identifier)it.next();
			if(id.s.equals(i.s)) return locals.get(id);
		}
		return null;
	}
	
	public Type getParam(Identifier i) {
		Iterator<Identifier> it = params.keySet().iterator();
		while(it.hasNext()) {
			Identifier id = (Identifier)it.next();
			if(id.s.equals(i.s)) return params.get(id);
		}
		return null;
	}
	
	public boolean containsLocal(Identifier i) {
		Iterator<Identifier> it = locals.keySet().iterator();
		while(it.hasNext()) {
			Identifier id = (Identifier)it.next();
			if(id.s.equals(i.s)) return true;
		}
		return false;
	}
	
	public boolean containsParam(Identifier i) {
		Iterator<Identifier> it = params.keySet().iterator();
		while(it.hasNext()) {
			Identifier id = (Identifier)it.next();
			if(id.s.equals(i.s)) return true;
		}
		return false;
	}
	
	public void print() {
		System.out.print("      " + name + "(");
		for(int i = 0; i < paramsTypes.size() - 1; i++) {
			System.out.print(paramsTypes.get(i) + ", ");
		}
		System.out.println(paramsTypes.get(paramsTypes.size() - 1) + ") {");
		Iterator<Identifier> it = locals.keySet().iterator();
		while(it.hasNext()) {
			Identifier id = (Identifier)it.next();
			System.out.println("         " + locals.get(id) + "\t" + id);
		}
		System.out.println("      }");
			
	}
}
